<template>
  <div>
    <div class="div1">
      <div class="div1-1">
        <router-link to="/"
          ><img src="../assets/imgs/xiaomilogo.png" class="imgLOGO" alt=""
        /></router-link>
      </div>
      <div class="div1-2">
        <span class="span1">我的购物车</span>
        <span class="span2"
          >温馨提示：产品是否购买成功，以最终下单为准哦，请尽快结算</span
        >
      </div>
      <div class="div1-3">
        <el-button type="text" style="color: black" v-if="Username">{{
          this.Username
        }}</el-button>
        <el-button
          type="text"
          style="color: black"
          @click="$router.push('/Load')"
          v-if="!Username"
          >登录</el-button
        >
        <el-button
          type="text"
          style="color: black"
          @click="$router.push('/Load')"
          v-if="!Username"
          >注册</el-button
        >
      </div>
    </div>
    <div class="div2">
      <div class="div2-1" v-if="Username">
        <el-table
          ref="multipleTable"
          :data="showCar"
          tooltip-effect="dark"
          style="width: 100%; font-size: 17px"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            width="80px"
            align="center"
            type="selection"
            aria-checked="true"
          >
          </el-table-column>

          <el-table-column
            width="100px"
            align="center"
            label=""
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <img :src="scope.row.Cimg" alt="" class="tabimg" />
            </template>
          </el-table-column>

          <el-table-column
            width="230px"
            align="center"
            prop="Cname"
            label="商品名称"
          >
          </el-table-column>

          <el-table-column
            width="220px"
            align="center"
            prop="Cprice"
            label="单价"
            show-overflow-tooltip
          >
          </el-table-column>

          <el-table-column
            width="260px"
            align="center"
            prop="Ssum"
            label="数量"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input-number
                v-model="scope.row.Ssum"
                @change="UpShoppingCarNum(scope.row.Scid, scope.row.Ssum)"
                :min="1"
                style="width: 150px"
              ></el-input-number>
            </template>
          </el-table-column>

          <el-table-column
            width="270px"
            align="center"
            label="小计"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <span style="color: #ff6700"
                >¥{{ scope.row.Cprice * scope.row.Ssum }}</span
              >
            </template>
          </el-table-column>

          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-close"
                style="font-size: 17px; color: #757575"
                @click="handleDelete(scope.$index, scope.row)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="div2-1-1" v-if="!Username">
        <div class="div2-1-1-1">
          <img src="../assets/imgs/kong.jpg" alt="">
        </div>
        <div class="div2-1-1-2">
          <p style="font-size: 40px;color: #B0B0B0;"> 您的购物车还是空的</p>
          <p style="font-size: 30px;color: #B0B0B0;"> 登录后将显示您之前加入的商品</p>
        </div>
      </div>
      <div class="div2-2">
        <el-button type="text" class="button3">继续购物</el-button>
        <div class="div2-2-2">
          共<span style="color: #ff6700">{{ couetsumb }}</span
          >件商品,已选择
          <span style="color: #ff6700">{{ countsuma }}</span> 件商品
        </div>
        <div class="div2-2-3">
          <span class="span3"
            >合计:<span class="span4">{{ countprice }}</span
            >元</span
          >
          <el-button class="button2" :disabled="disable" @click="tiaoChenkout()"
            >去结算</el-button
          >
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      Username: "",
      showCar: {},     
      disable: false, //购买按钮状态
      countsuma: 0, //已选择
      couetsumb: 0, //总数
      countprice: 0, //总价
      multipleSelection: [], //全选状态
      Check: {
        AllSelection:[],
        Sumprice:'',
        Sumsumaa:''
      },
    };
  },
  methods: {
    showCart() {
      this.$axios
        .get(
          "http://localhost:16673/api/Cart/showCart?Uid=" +
            sessionStorage.getItem("Uid")
        )
        .then((res) => {
          this.couetsumb = res.data.couetsumb;
          this.showCar = res.data.a;
          this.$nextTick(() => {
            this.toggleSelection(this.showCar);
          });
        });
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.countsuma = 0;
      this.countprice = 0;
      this.multipleSelection = val;
      this.multipleSelection.filter((item) => {
        this.countsuma += item.Ssum;
        this.countprice += item.Cprice * item.Ssum;
      });
    },
    //数量同步
    UpShoppingCarNum(Scid, Ssum) {
      console.log(Scid);
      console.log(Ssum);
      this.$axios
        .post(
          `http://localhost:16673/api/Cart/UpShoppingCarNum?Scid=${Scid} &Ssum=${Ssum}`
        )
        .then((res) => {
          if (res.data > 0) {
            this.showCart();
          }
        });
    },

    tiaoChenkout() {
      this.Check.AllSelection=this.multipleSelection;
      this.Check.Sumprice=this.countprice
      this.Check.Sumsumaa=this.countsuma
      this.$router.push({
        path: "/Checkout",
        query: { Check: JSON.stringify(this.Check) },
      });
    },
  },
  created: function () {
    this.showCart();
    this.Username = sessionStorage.getItem("Username");
  },
  mounted() {},
};
</script>

<style scoped>
.div1 {
  margin: 0 auto;
  width: 100%;
  height: 100px;
  margin-top: 8px;
  border-bottom: 3px solid #ff6700;
}
.div1-1 {
  float: left;
  margin-top: 5px;
}
.div1-2 {
  float: left;
  height: 100px;
  line-height: 90px;
  margin-left: 30px;
}
.div1-3 {
  float: right;
  line-height: 100px;
  margin-right: 135px;
}
.imgLOGO {
  width: 65px;
  margin-left: 125px;
  margin-top: 7px;
}
.span1 {
  color: #424242;
  font-size: 30px;
}
.span2 {
  font-size: 13px;
  color: #757575;
  margin-left: 16px;
}
.div2 {
  width: 100%;
  height: 900px;
  background-color: #f5f5f5;
}
.div2-1 {
  width: 1250px;
  margin: 0 auto;
  padding-top: 70px;
}
.tabimg {
  width: 90px;
}
.div2-2 {
  width: 1250px;
  height: 50px;
  padding-top: 20px;
  padding-bottom: 20px;
  background-clip: content-box;
  background-color: white;
  line-height: 50px;
  float: left;
  margin-left: 127px;
}
.button3 {
  font-size: 20px;
  margin-left: 40px;
  color: #424242;
  float: left;
  padding: 0;
  line-height: 50px;
}
.div2-2-2 {
  float: left;
  margin-left: 40px;
}
.div2-2-3 {
  float: right;
}
.span3 {
  margin-right: 30px;
  font-size: 18px;
  color: #ff6700;
}
.span4 {
  margin-left: 5px;
  font-size: 28px;
}
.button2 {
  width: 200px;
  height: 50px;
  border-radius: 0px;
  border: 0px;
  background-color: #ff6700;
  color: white;
  font-size: 20px;
  vertical-align: top;
  line-height: -100px;
}
.div2-1-1{
  width:1250px ;
  float: left;
  margin-left: 150px;
  background-color: #F5F5F5;
}
.div2-1-1-1{
  margin-left: 200px;
  float: left;
  background-color: #F5F5F5;
}
.div2-1-1-2{
margin-top: 100px;
float: left;
background-color: #F5F5F5;
}
</style>

<style >
.el-input-number__decrease,
.el-input-number__increase {
  background-color: #dcdfe600;
  border-right: #dcdfe600;
  border-left: #dcdfe600;
}
.el-checkbox__input.is-checked .el-checkbox__inner,
.el-checkbox__input.is-indeterminate .el-checkbox__inner {
  background-color: #ff6700;
  border-color: #ff6700;
}
</style>